package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Message;

public class MessageDao extends DAO{
    private Connection conn = null;
    private PreparedStatement pst = null;
    ResultSet rs = null;
    ResultSetMetaData rd = null;
    public boolean insert(String userName,String message,String mDate) {
        boolean flag = false;
        try {
            conn = this.getConnection();
            pst = conn.prepareStatement("insert into tb_message (uName,message,mDate) values(?,?,?)");
            pst.setString(1, userName);
            pst.setString(2,message);
            pst.setString(3, mDate);
            flag = pst.execute();
            pst.close();
            conn.close();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("留言插入出错，"+e.getMessage());
        }
        return flag;
    }

    public List<Message> query() {
        List<Message> mList = new ArrayList<Message>();
        Message message;
        try {
            conn = this.getConnection();
            pst = conn.prepareStatement("select uName,message,mDate from tb_message");
            rs = pst.executeQuery();
            while(rs.next()) {
                message = new Message();
                message.setuName(rs.getString(1));
                message.setMessage(rs.getString(2));
                message.setmDate(rs.getString(3));
                mList.add(message);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("留言查询出错，"+e.getMessage());
        }
        return mList;
    }
}
